<?php
require_once dirname(dirname(__FILE__)) . '/shared/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_bet_calculators.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_matches.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_bet_history.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_session.php';
require_once DOCUMENT_ROOT . DIR_INCLUDE . 'html_generator.php';

lib_session::start_session();
$username_main = $_SESSION['user_info']['username'];

$match_id = filter_input(INPUT_POST, "match_id");
$game_id = filter_input(INPUT_POST, "game_id");
if (!empty($game_id)) { // for roulette
    $match_id = api_roulette::get_sub_match_id_by_game_id_and_parent_id($game_id, $match_id);
}

$type_id = api_matches::get_type_id_by_match_id($match_id);
$list = api_bet_calculators::get_all_bet_history_by_match_id($match_id);
?>
            <div class = "col-md-offset-1 col-md-10">
                <div class = "service-wrapper">
                    <table class="table">
                        <tr>
                            <?php
                            if ($type_id == 2) {
                            ?>
                            <th class="text-center">Game</th>
                            <?php
                            }
                            ?>
                            <th class = "text-center">Choice</th>
                            <th class = "text-center">Amount</th>
                            <th class = "text-center">Profit If Win</th>
                            <th class = "text-center">Total If Win</th>
                            <th class = "text-center">Username</th>
                            <th class = "text-center">Remove</th>
                            <th class = "text-center">Result</th>
                        </tr>
                        
                            <?php
                            
                            
                            if (empty($list) || count($list) == 0) {
                                echo "<td colspan='9'>Empty Betting.</td>";
                            } else {
                                foreach ($list as $item) {
                            ?>
                        <tr>
                            <?php
                            if ($type_id == 2) {
                            ?>
                            <td class="text-center"><?php echo $item['game_name']; ?></td>
                            <?php
                            }
                            ?>
                            <td class="text-center"><?php echo $item['choice_name']; ?></td>
                            <td class="text-center">$<?php echo $item['bet_amount']; ?></td>
                            <?php
                            $result = show_profit_text($item['profit_if_win'], $item['bet_amount'], $match_id, FALSE);
                            ?>
                            <td class="text-center <?php echo $result['class']; ?>"><?php echo $result['profit_if_win_text']; ?></td>
                            <td class="text-center"><?php echo $result['total_if_win_text']; ?></td>
                            <td><?php echo $item['username']; ?></td>
                            <td>
                                <?php 
                                if ($username_main === "admin" || $username_main === $item['username']) {
                                ?>
                                <a href="javascript: void(0);" onclick="return betLoading.deleteBetting(<?php echo $item['id'].",".$item['bet_amount']; ?>);">
                                    <img class="ts-action-icon center-block" src="<?php echo LINK_ROOT . DIR_SHARED_LAYOUT_IMAGES_ICONS . 'delete.png' ?>" alt="delete">
                                </a>
                                <?php
                                }
                                ?>
                            </td>
                            <?php
                            $result_bet       = api_bet_history::get_result_bet_of_match($item['match_id'], $item['id']);
                            $result_money = api_bet_history::get_result_money($result_bet, $item['bet_amount'], $result['profit_if_win_value']);
                            ?>
                            <td class="text-center <?php echo $result_money['class']; ?>"><?php echo $result_money['money_text']; ?></td>
                        </tr>
                            <?php
                                }
                            } ?>
                        
                    </table>
                </div>
            </div>
